package com.gexiaobao.pigeon.ui.pigeoncatcher

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityCarInfoBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterCarInfo
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityAddCarInfo
import com.gexiaobao.pigeon.viewmodel.PigeonCatcherViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/7/3 10:27
 * @Describe    : 车辆信息
 */
class ActivityCarInfo : BaseActivity<PigeonCatcherViewModel, ActivityCarInfoBinding>() {

    private val mAdapter: AdapterCarInfo by lazy { AdapterCarInfo() }
    private var mOrgId: Int = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "车辆信息"

        mOrgId = intent.getIntExtra("orgId", 0)

        onRefresh()
        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 10f)))

        eventViewModel.toRefreshDataStr.observeInActivity(this) {
            if (it == "ActivityAddCarInfo") {
                onRefresh()
            }
        }

        mAdapter.addChildClickViewIds(R.id.llDeleteCarInfo)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.llDeleteCarInfo -> {
                    showDeleteDialog(mAdapter.data[position].id.toString())
                }
            }
        }

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }
    }

    private fun showDeleteDialog(id: String) {
        val dialog = RxDialogDefault(this)
        dialog.setContent("确认要删除掉当前车辆信息吗？")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            mViewModel.deleteTransporterApplyCar(id)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getTransporterApplyCarList()
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 查询接鸽员申请中关于汽车的信息列表  */
        mViewModel.deleteCarResult.observe(this) {
            RxToast.showToast("已删除")
            onRefresh()
        }
        /** 查询接鸽员申请中关于汽车的信息列表  */
        mViewModel.carInfoListResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.tvAddCar.isVisible = it.list.size < 3
            if (it != null) {
                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.tvAddCar) {
            when (it) {
                mDatabind.tvAddCar -> {
                    startActivity<ActivityAddCarInfo>("orgId" to mOrgId, "type" to "ActivityCarInfo")
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }
}